package cn.kgc.springmvc.day03.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author 课工场
 * @date 2024/7/22
 * @description springmvc环境下如何实现转发和重定向
 * 转发实现方案:
 * 1.  String  转发
 * 2.  ModelAndView   转发
 * 3.  HttpServletRequest   转发
 * 4.  forward              转发    springmvc
 * <p>
 * 重定向的实现方案:
 * 1.  HttpServletResponse    重定向
 * 2.  redirect               重定向      springmvc
 */

@Controller
@RequestMapping("fr")
public class FRController {

    @GetMapping("m1")
    public void method1(HttpServletRequest request, HttpServletResponse response) throws Exception{
        request.setAttribute("info","hello request");
        request.getRequestDispatcher("/user.jsp").forward(request,response);
    }

    //  forward 关键词添加之后 不会在经过视图解析器   视图的全路径
    @GetMapping("m2")
    public String method2(){

        return "forward:/user.jsp";
    }

    @GetMapping("m3")
    public void method3(HttpServletRequest request, HttpServletResponse response) throws Exception{

        HttpSession session = request.getSession();
        session.setAttribute("info","hello HttpServletResponse ");

        // 重定向
        response.sendRedirect(request.getContextPath()+"/user.jsp");

    }

    //  redirect   重定向不经过视图解析器   redirect:/user.jsp       /   标识项目的根路径
    @GetMapping("m4")
    public String method4(HttpSession session) throws Exception{
        session.setAttribute("info","hello HttpSession ");
        return "redirect:/user.jsp";
    }

}
